#region Using

using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.Specialized;
using BlogEngine.Core;
using BlogEngine.Core.Providers.EloqueraProvider.Db;
using Eloquera.Client;
using System.Collections;

#endregion

namespace BlogEngine.Core.Providers {
    /// <summary>
    /// A storage provider for BlogEngine that uses XML files.
    /// <remarks>
    /// To build another provider, you can just copy and modify
    /// this one. Then add it to the web.config's BlogEngine section.
    /// </remarks>
    /// </summary>
    public partial class EloqueraBlogProvider : BlogProvider {

        #region Categories

        /// <summary>
        /// Gets a Category based on a Guid
        /// </summary>
        /// <param name="id">The category's Guid.</param>
        /// <returns>A matching Category</returns>
        public override Category SelectCategory(Guid id) {
            List<Category> categories = Category.Categories;
            Category category = new Category();
            foreach (Category cat in categories) {
                if (cat.Id == id)
                    category = cat;
            }
            category.MarkOld();
            return category;
        }

        /// <summary>
        /// Inserts a Category
        /// </summary>
        /// <param name="category">Must be a valid Category object.</param>
        public override void InsertCategory(Category category) {
            List<Category> categories = Category.Categories;
            categories.Add(category);
            EqCategory cat = new EqCategory() {
                Id = category.Id,
                Parent = category.Parent.Value,
                Title = category.Title,
                Description = category.Description
            };
            EqDb.Store(cat);
        }

        /// <summary>
        /// Updates a Category
        /// </summary>
        /// <param name="category">Must be a valid Category object.</param>
        public override void UpdateCategory(Category category) {
            Parameters param = EqDb.CreateParameters();
            param["id"] = category.Id;
            EqCategory cat = (EqCategory)EqDb.ExecuteScalar("select EqCategory where Id = @id", param);
            if (cat != null) {
                cat.Description = category.Description;
                cat.Parent = category.Parent.Value;
                cat.Title = category.Title;
                EqDb.Store(cat);
            }
        }


        /// <summary>
        /// Deletes a Category
        /// </summary>
        /// <param name="category">Must be a valid Category object.</param>
        public override void DeleteCategory(Category category) {
            Parameters param = EqDb.CreateParameters();
            param["id"] = category.Id;
            EqCategory cat = (EqCategory)EqDb.ExecuteScalar("select EqCategory where Id = @id", param);
            List<Category> categories = Category.Categories;
            categories.Remove(category);
            EqDb.Delete(cat);
        }

        /// <summary>
        /// Fills an unsorted list of categories.
        /// </summary>
        /// <returns>A List&lt;Category&gt; of all Categories.</returns>
        public override List<Category> FillCategories() {
            List<Category> categories = new List<Category>();
            IEnumerable eqCategories = EqDb.ExecuteQuery("select EqCategory");
            foreach (var obj in eqCategories) {
                EqCategory eqCategory = (EqCategory)obj;
                Category category = new Category() {
                    Id = eqCategory.Id,
                    Parent = eqCategory.Parent,
                    Title = eqCategory.Title,
                    Description = eqCategory.Description
                };
                categories.Add(category);
                category.MarkOld();
            }
            return categories;
        }

        #endregion

    }
}
